package com.xxgc.vfc.auth.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxgc.vfc.api.dto.UserLoginDTO;
import com.xxgc.vfc.api.vo.LoginByPhoneAndPassVO;
import com.xxgc.vfc.api.vo.UserRegisterByPhoneVO;
import com.xxgc.vfc.auth.entity.Users;
import com.xxgc.vfc.auth.mapper.UsersMapper;
import com.xxgc.vfc.auth.service.IUsersService;
import com.xxgc.vfc.common.error.custom.NameOrPassException;
import com.xxgc.vfc.common.error.custom.UserPhoneAlreadyExistsException;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 风清杨
 * @since 2023-11-13
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {

    @Override
    public boolean userRegisterByPhone(UserRegisterByPhoneVO userRegisterByPhoneVO) throws UnsupportedEncodingException {
        //查重
        //查询条件
        QueryWrapper<Users> qw = new QueryWrapper<>();
        qw.eq("phone",userRegisterByPhoneVO.getPhone());
        Users one = this.getOne(qw);
        if(ObjectUtil.isNotNull(one)){
            throw new UserPhoneAlreadyExistsException().errorMsg("手机号已被注册");
        }
        //密码加密  MD5
        Digester md5 = new Digester(DigestAlgorithm.MD5);
        //获取随机盐值
        String simpleUUID = IdUtil.simpleUUID();
        byte[] salt = simpleUUID.getBytes("UTF8");
        //加盐操作
        md5.setSalt(salt);
        // 加密之后的
        String digestHex = md5.digestHex(userRegisterByPhoneVO.getPassWord());
        //加密之后密码放回去
        userRegisterByPhoneVO.setPassWord(digestHex);
        Users users = new Users();
        //VO的字段转给Entity？
        BeanUtil.copyProperties(userRegisterByPhoneVO, users);
        //把盐放进去
        users.setSalt(simpleUUID);
        //Dao层
        this.save(users);
        return true;
    }

    //登录
    @Override
    public UserLoginDTO loginByPhoneAndPass(LoginByPhoneAndPassVO loginByPhoneAndPassVO) throws UnsupportedEncodingException {
        //通过手机号查询用户
        QueryWrapper<Users> qw = new QueryWrapper<>();
        qw.eq("phone",loginByPhoneAndPassVO.getPhone());
        Users one = this.getOne(qw);
        if(ObjectUtil.isNull(one)){
            //抛出异常，用户名密码错误异常
            throw new NameOrPassException().errorMsg("用户名或密码错误");
        }
        //之前加密出来的内容
        String passWord = one.getPassWord();
        //之前加密用的盐
        String salt = one.getSalt();
        //没有加密过的密码
        String oldPass = loginByPhoneAndPassVO.getPassWord();

        //密码加密  MD5
        Digester md5 = new Digester(DigestAlgorithm.MD5);
        byte[] saltByte = salt.getBytes("UTF8");
        //加盐操作
        md5.setSalt(saltByte);
        // 加密之后的
        String newPass = md5.digestHex(oldPass);
        //如果两次加密结果不一致
        if(!newPass.equals(passWord)){
            //抛出异常，用户名密码错误异常
            throw new NameOrPassException().errorMsg("用户名或密码错误");
        }
        UserLoginDTO userLoginDTO = new UserLoginDTO();
        BeanUtil.copyProperties(one,userLoginDTO);
        return userLoginDTO;
    }
}
